package com.goldsprite.microhorigame.screens.examples.simplephysexamples;

import com.goldsprite.gameframeworks.log.Debug;
import com.goldsprite.gameframeworks.simplephys.shape.PhysRect;
import com.goldsprite.gameframeworks.utils.math.Vector2;
import com.goldsprite.gameframeworks.simplephys.collis.CollisionResult;
import com.goldsprite.gameframeworks.simplephys.collis.CollisionUtils;

import java.util.List;

// 在你的游戏实体中使用
public class GameEntity {
	public final PhysRect bounds = new PhysRect();
	public final Vector2 velocity = new Vector2();
	private boolean collied;

	public void update(float delta, List<GameEntity> others) {
		// 应用速度
		bounds.x += velocity.x * delta;
		bounds.y += velocity.y * delta;

		// 检测与所有其他实体的碰撞
		boolean hasCollision = false;
		for (GameEntity other : others) {
			if (this == other) continue;

			CollisionResult collisionResult = CollisionUtils.calculateCollision(this.bounds, other.bounds);
			if (collisionResult != null) {
				hasCollision = true;
				// 发生碰撞！处理碰撞响应
				onCollisionDetected(other, collisionResult);
			}
		}
		this.collied = hasCollision;
	}

	protected void onCollisionDetected(GameEntity other, CollisionResult result) {
		collisionBlock(other, result);
		Debug.logT("CollisionDemo", "碰撞阻挡: " + result);
	}

	protected void collisionBlock(GameEntity other, CollisionResult result) {
		// 位置修正：将物体推出穿透区域
		bounds.x += result.normal.x * result.depth;
		bounds.y += result.normal.y * result.depth;
	}

	public boolean isCollied() {
		return collied;
	}
}
